#include <cstdio>
#include <unistd.h>
#include <queue>
#include <thread>

#include <promise.h>

sptr<Promise<int>> Async(int time)
{
    sptr<Promise<int>> promise = new Promise<int>;
    std::thread thread([time, promise]() {
        sleep(time);
        promise->Resolve(10 - time);
    });
    thread.detach();
    return promise;
}

int main()
{
    Async(3)->Then([](int ret) {printf("%d\n", ret);});
    auto p1 = Async(2);
    auto p2 = Async(3);
    auto p3 = Async(4);
    auto p4 = Async(4);

    printf("race: %d\n", Promise<int>::Race({p1, p2, p3, p4})->Await());
    sleep(10);
    return 0;
}
